package com.lly.pro.repositories;

import com.lly.pro.entity.Admin;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Hommond
 * @date 2/24/2019
 */
public interface AdminRepository extends JpaRepository<Admin,Long> {
    /**
     * 根据userId获取Admin信息（密码除外）
     * @Param userId
     * @return AdminInfo
     */
    Admin findByUserId(long userId);

    @Transactional(rollbackFor = Exception.class)
    @Query(value = "UPDATE Admin u SET u.password=?1 WHERE u.userId=?2 ")
    int updatePasswordByUserId(String password,String userId);

    /**
     * 用户名，密码是否正确
     * @Param userName 用户名
     * @Param password 密码
     * @return
     */
    @Query(value = "select count(u) from Admin u where u.userName = ?1 and u.password = ?2")
    Integer findByUserNameAndPassword(String userName,String password);

    /**
     * 根据用户名和密码获取userId
     * @Param userName 用户名
     * @Param password 密码
     * @return
     */
    @Query(value = "select u.userId from Admin u where u.userName = ?1 and u.password = ?2")
    List findUserId(String userName, String password);

    /**
     * 根据用户id和密码获取userId
     * @Param userId 用户id
     * @Param password 密码
     * @return
     */
    @Query(value = "SELECT COUNT(u) FROM Admin u WHERE u.userId=?1 AND u.password=?2")
    Long findByUserIdAndPassword(Long userId, String password);
}
